import { glob, globSync, globStream } from 'glob'
import { resolve } from 'path'
const localFiles = globSync('./pages/**/index.html')

// 自动载入pages下的index.html文件 打包使用 npm run build -pages=xx,xx,xx
// const original = JSON.parse(process.env.npm_config_argv).original // build 后面的参数
// node > v16.18.0
const params = process.env.npm_config_pages
  ? process.env.npm_config_pages.split(',')
  : []
let localPage = {},
  pages = {}
localFiles.forEach((url) => {
  let reg = /pages\/(\w+)/
  let name = url.match(reg)[0].replace('pages/', '')
  localPage[name] = resolve(__dirname, url)
})

if (process.versions.node >= '16.18.0') {
  if (params.length) {
    params.map((name) => {
      if (localPage[name]) {
        pages[name] = localPage[name]
      }
    })
  } else {
    pages = localPage
  }
} else {
  pages = localPage
}

export default pages
